/*
 * This project is mainly focused on developing more and more utilisties
 *  like mail utilities, dao utilities, ldap utilities, file utilities, 
 *  file importing utilities etc. for easy coding in java.
 *  
 *  Copyright (C) 2012  R.Satheesh Kumar alias GURU
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jsmart.util.io;

import org.jsmart.util.core.Utils;
import org.jsmart.util.core.UtilsException;
import org.jsmart.util.RowMapper;
import org.jsmart.util.core.AbstractUtils;
import org.jsmart.util.core.AbstractItemCallback;
import org.jsmart.util.core.AbstractItemFilter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * This class is used for accessing the reader object for file handling
 * purposes and reading the file very easily with single line of code which
 * supplies the filter option and the business logic callback which must
 * be applied on the each line of file content.
 * @author R.Satheesh Kumar alias GURU {pulsarcoder@gmail.com}
 * @since v1.0
 * @see Utils
 * @see AbstractUtils
 */
public abstract class ReaderUtils {
    public static <U> List<U> query(final BufferedReader reader,
            final AbstractItemCallback<U, String> callback,
            final AbstractItemFilter<String> filter,
            Boolean singleData) throws UtilsException {
        return AbstractUtils.execute(reader, new QueryReaderCaller<U>(callback, filter, singleData));
    }

    public static <U> List<U> query(final BufferedReader reader,
            final AbstractItemCallback<U, String> callback,
            final AbstractItemFilter<String> filter) throws UtilsException {
        return query(reader, callback, filter, false);
    }

    public static <U> List<U> query(final BufferedReader reader,
            final AbstractItemCallback<U, String> callback) throws UtilsException {
        return query(reader, callback, new AbstractUtils.SimpleAbstractItemFilter<String>());
    }

    public static <U> List<U> query(final BufferedReader reader,
            final RowMapper<U, String> rowMapper,
            final AbstractItemFilter<String> filter) throws UtilsException {
        return query(reader, new AbstractUtils.SimpleAbstractItemCallback<U, String>(rowMapper), filter);
    }

    public static <U> List<U> query(final BufferedReader reader,
            final RowMapper<U, String> rowMapper) throws UtilsException {
        return query(reader, rowMapper, new AbstractUtils.SimpleAbstractItemFilter<String>());
    }

    public static <U> List<U> query(final InputStream in,
            final AbstractItemCallback<U, String> callback,
            final AbstractItemFilter<String> filter,
            Boolean singleData) throws UtilsException {
        return query(new BufferedReader(new InputStreamReader(in)), callback, filter, singleData);
    }

    public static <U> List<U> query(final InputStream in,
            final AbstractItemCallback<U, String> callback) throws UtilsException {
        return query(in, callback, new AbstractUtils.SimpleAbstractItemFilter<String>(), false);
    }

    public static <U> List<U> query(final InputStream in,
            final RowMapper<U, String> rowMapper,
            final AbstractItemFilter<String> filter) throws UtilsException {
        return query(in, new AbstractUtils.SimpleAbstractItemCallback<U, String>(rowMapper), filter, false);
    }

    public static <U> List<U> query(final InputStream in,
            final RowMapper<U, String> rowMapper) throws UtilsException {
        return query(in, rowMapper, new AbstractUtils.SimpleAbstractItemFilter<String>());
    }

    public static <U> List<U> query(final File file,
            final AbstractItemCallback<U, String> callback,
            final AbstractItemFilter<String> filter,
            Boolean singleData) throws UtilsException {
        try {
            return query(new FileInputStream(file), callback, filter, singleData);
        }catch(Exception e) {
            throw new UtilsException("Exception in performing the business logic with file, due to "+e);
        }
    }

    public static <U> List<U> query(final File file,
            final AbstractItemCallback<U, String> callback) throws UtilsException {
        return query(file, callback, new AbstractUtils.SimpleAbstractItemFilter<String>(), false);
    }

    public static <U> List<U> query(final String fileName,
            final AbstractItemCallback<U, String> callback,
            final AbstractItemFilter<String> filter,
            Boolean singleData) throws UtilsException {
        return query(new File(fileName), callback, filter, singleData);
    }

    public static <U> List<U> query(final String fileName,
            final AbstractItemCallback<U, String> callback) throws UtilsException {
        return query(fileName, callback, new AbstractUtils.SimpleAbstractItemFilter<String>(), false);
    }

    public static <U> List<U> query(final String fileName,
            final RowMapper<U, String> rowMapper,
            final AbstractItemFilter<String> filter,
            Boolean singleData) throws UtilsException {
        return query(fileName, AbstractUtils.getSimpleItemCallback(rowMapper), filter, singleData);
    }

    public static <U> List<U> query(final String fileName,
            final RowMapper<U, String> rowMapper,
            Boolean singleData) throws UtilsException {
        return query(fileName, rowMapper, new AbstractUtils.SimpleAbstractItemFilter<String>(), singleData);
    }

    public static <U> List<U> query(final String fileName,
            final RowMapper<U, String> rowMapper) throws UtilsException {
        return query(fileName, rowMapper, false);
    }

    public static <U> U queryForObject(final String fileName,
            final RowMapper<U, String> rowMapper) throws UtilsException {
        return query(fileName, rowMapper, true).get(0);
    }
}
